package com.elvin.zoe.parser.sms;

import java.util.ArrayList;
import java.util.List;

import com.elvin.zoe.pojo.Message;

public class SmsGmailParser extends SmsParser {

    protected String getMessagingWith(String originalHtml) {
        String token = "SMS with ";
        int tokenStart = originalHtml.indexOf(token) + token.length();
        int tokenEnd = originalHtml.indexOf('<', tokenStart);
        return originalHtml.substring(tokenStart, tokenEnd).trim();
    }

    protected List<String> getMessageTokens(String originalHtml) {
        String messageStartToken = "<hr><table width=100% cellpadding=0 cellspacing=0 border=0>";
        String messageEndToken = "</table></table>";
        List<String> messageTokens = new ArrayList<String>();
        int tokenStart = originalHtml.indexOf(messageStartToken);
        while (tokenStart > 0) {
            int tokenEnd = originalHtml.indexOf(messageEndToken, tokenStart) + messageEndToken.length();
            messageTokens.add(originalHtml.substring(tokenStart, tokenEnd));
            tokenStart = originalHtml.indexOf(messageStartToken, tokenEnd);
        }
        return messageTokens;
    }

    @Override
    protected int getMessageType(String messageToken) {
        String typeToken = "telenths@gmail.com&lt;telenths@gmail.com&gt;";
        if (messageToken.indexOf(typeToken) >= 0) {
            return Message.OUTGOING;
        } else {
            return Message.INCOMMING;
        }
    }

    @Override
    protected String getMessageDatetime(String messageToken) {
        String datetimeStartToken = "<td align=right><font size=-1><b>";
        int tokenStart = messageToken.indexOf(datetimeStartToken) + datetimeStartToken.length();
        int tokenEnd = messageToken.indexOf("</b>", tokenStart);
        return messageToken.substring(tokenStart, tokenEnd);
    }

    @Override
    protected String getMessageDatetimeFormat() {
        return "EEE, MMM d, yyyy 'at' hh:mm aaa";
    }
    
    @Override
    protected String getMessageText(String messageToken) {
        String textStartToken = "<font size=-1>";
        int tokenStart = messageToken.lastIndexOf(textStartToken) + textStartToken.length();
        int tokenEnd = messageToken.indexOf("</font>", tokenStart);
        return messageToken.substring(tokenStart, tokenEnd);
    }

    @Override
    protected String getMessageImageName(String messageToken){
        String textStartToken = "<td><b>";
        int tokenStart = messageToken.lastIndexOf(textStartToken) + textStartToken.length();
        if(tokenStart <= textStartToken.length() -1)
            return null;
        int tokenEnd = messageToken.indexOf("</b><br/>", tokenStart);
        return messageToken.substring(tokenStart, tokenEnd);
    }

}
